﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BaocaoNPC.Usercontrols.reports;
//using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using BaocaoNPC.common;
using Component;
using CMISLibrary;
using bus_TienichNPC;

namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for DS_THUTIEN_ATM.xaml
    /// </summary>
    public partial class DS_THUTIEN_ATM_TNGAN : UserControl
    {
        public DataSet ds_DM_NH = new DataSet();
        public DataSet ds_DM_PTTT = new DataSet();
        public DataSet ds_DM_TNGAN = new DataSet();
        public DataSet ds_KHNOPTIEN = new DataSet();
        public DataSet ds_KHNOPTIEN_NH = new DataSet();
        public DataSet ds_KHNOPTIEN_NH_All = new DataSet();
        public DataSet ds_KHNOPTIEN_NPC = new DataSet();
        public DataSet ds_KHNOPTIEN_NPC_All = new DataSet();
        public DataSet ds_KHNOPTIEN_KHOP = new DataSet();
        //public DataSet ds_KHLECH = new DataSet();
        public long pcount = 0;//so cong to duoc chon
        public long ptcount = 0;// tong so cong to load ra luoi
        public long ptien = 0;
        public Int16 i16ThangHT = 0;
        public Int16 i16NamHT = 0;
        public Int16 i16Hour = 16;
        public Int16 i16Munite = 30;
        public int int_ttrang_no = 0;
        public DateTime? ptungay, pdenngay;
        public string strmanh,strmapttt;        
        public String strMadviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        public string strUsername = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "NPC_ATM";

        public DS_THUTIEN_ATM_TNGAN()
        {
            try
            {
                InitializeComponent();
                Component.TabIndexManager.RegisterTabManagerWithSelectAll(this);
                ds_DM_NH = GlobalServices.Service_NPCUS.get_DM_NHANG();
                ds_DM_PTTT = GlobalServices.Service_NPCUS.get_PHTHUC_TTOAN();
                ds_DM_TNGAN = GlobalServices.Service_NPCUS.get_DM_TNGAN(strMadviqly);
                txtthungan.AddItem(new CMIS2_TextBox.AutoCompleteEntry(ds_DM_TNGAN));
                dtpngaynop_den.SelectedDate = DateTime.Now;
                dtpngaynop_tu.SelectedDate = DateTime.Now;
                
                GET_THANGNAM_HT();
                GET_TIMESPAN();
                txthour.Text = i16Hour.ToString();
                txtmunite.Text = i16Munite.ToString();
                
                if (i16ThangHT.ToString().Length == 1)
                {
                    dtpThangNamHT.Text = "0" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
                }
                else
                {
                    dtpThangNamHT.Text = i16ThangHT.ToString() + "/" + i16NamHT.ToString(); 
                }
               
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }
        private void GET_THANGNAM_HT()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "CN" && dr["MA_DVIQLY"].ToString() == strMadviqly)
                        {
                            i16ThangHT = Convert.ToInt16(dr["THANG"]);
                            i16NamHT = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                i16ThangHT = Convert.ToInt16(DateTime.Now.Month);
                i16NamHT = Convert.ToInt16(DateTime.Now.Year);
            }
        }
        private void GET_TIMESPAN()
        {
            try
            {
                DataSet dstime = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dstime = GlobalServices.Service_NPCUS.get_TIMESPAN(strMadviqly);
                if (dstime != null && dstime.Tables.Count > 0)
                {

                    i16Hour = Convert.ToInt16(dstime.Tables[0].Rows[0]["GIO"]);
                    i16Munite = Convert.ToInt16(dstime.Tables[0].Rows[0]["PHUT"]);      
                }

            }
            catch
            {
                i16Hour = 16;
                i16Munite = 30;
            }
        }
        private void btnload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.Wait;
               
               

                ptungay = dtpngaynop_tu.SelectedDate;
                pdenngay = dtpngaynop_den.SelectedDate;
                string strMatngan = txtthungan.Text;

                #region checkngay
                if (pdenngay == null)
                    showMessErrorOnView("Điều kiện ngày nộp không được để trống", dtpngaynop_den);
                if (ptungay == null)
                    showMessErrorOnView("Điều kiện ngày nộp không được để trống", dtpngaynop_tu);
                if (ptungay > pdenngay)
                    showMessErrorOnView("Điều kiện ngày nộp không hợp lệ", dtpngaynop_tu);
                #endregion

                TimeSpan ts = new TimeSpan(i16Hour, i16Munite, 0);
                ptungay = ptungay.Value.AddDays(-1) + ts;
                pdenngay = pdenngay + ts;
                
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //ds_KHNOPTIEN = bus.get_dsKH_thuATM_TNGAN(strMadviqly, strMatngan, ptungay, pdenngay);
                ds_KHNOPTIEN = GlobalServices.Service_NPCUS.get_dsKH_thuATM_TNGAN(strMadviqly, strMatngan, ptungay, pdenngay);
                //ds_KHNOPTIEN.WriteXmlSchema("D:\\Projects\\NPCUS\\ATM\\DSKHNOPTIENTNGAN.xsd");
                //ds_KHNOPTIEN = GlobalServices.Service_NPCUS.get_dsKH_thuATM(strMadviqly, strmanh, strmapttt, ptungay, pdenngay, int_ttrang_no);
                if (ds_KHNOPTIEN != null)
                    if (ds_KHNOPTIEN.Tables.Count > 0)
                        if (ds_KHNOPTIEN.Tables["DSCHAMNO"].Rows.Count > 0)
                        {
                            dgrdsthutien.ItemsSource = ds_KHNOPTIEN.Tables[0].DefaultView;
                            ds_KHNOPTIEN.Tables[0].DefaultView.AllowNew = false;
                            
                        }
                        else
                        { showMessErrorOnView("Không tìm thấy thông tin theo điều kiện chọn", dtpngaynop_tu); return; }
                    else { showMessErrorOnView("Không tìm thấy thông tin theo điều kiện chọn", dtpngaynop_tu); return; }
                else { showMessErrorOnView("Không tìm thấy thông tin theo điều kiện chọn", dtpngaynop_tu); return; }
                for (Int32 i = 0; i < ds_KHNOPTIEN.Tables["DSCHAMNO"].Rows.Count; i++)
                {
                    ptien = ptien + Convert.ToInt64(ds_KHNOPTIEN.Tables["DSCHAMNO"].Rows[i]["TTIEN_NOP"]);
                }
                ptcount = ds_KHNOPTIEN.Tables["DSCHAMNO"].Rows.Count;
                pcount = ds_KHNOPTIEN.Tables["DSCHAMNO"].Rows.Count;
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
                btnin.IsEnabled = true;
                
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
            this.Cursor = Cursors.Arrow;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            btnin.IsEnabled = false;
            btnload.IsEnabled = true;
        
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                DataRowView drv_khnoptien = dgrdsthutien.SelectedItem as DataRowView;
                if (drv_khnoptien == null)
                { return; }
                if (dgrdsthutien.CurrentCell.Column == null)
                { return; }
                if (drv_khnoptien != null)
                {
                    if (dgrdsthutien.CurrentCell.Column.HeaderStringFormat.ToUpper() == "CHON")
                    {
                        if (Convert.ToBoolean(drv_khnoptien["CHON"]) == false)
                        {
                            pcount = pcount - 1;
                            ptien = ptien - Convert.ToInt64(drv_khnoptien["TTIEN_NOP"].ToString());
                            //DataView dv = dgrdsthutien.ItemsSource as DataView;
                            //DataRow[] arrSelect = dv.Table.Select("CHON = 1");
                            //DataRow[] arrUnSelect = dv.Table.Select("CHON = 0");
                            //pcount = arrSelect.Length;
                            //ptien = 0;
                            //for (Int32 i = 0; i < arrSelect.Length; i++)
                            //{ ptien = ptien + Convert.ToInt64(arrSelect[i]["TTIEN_NOP"]); }
                            //for (Int32 j = 0; j < arrUnSelect.Length; j++)
                            //{ ptien = ptien - Convert.ToInt64(arrUnSelect[j]["TTIEN_NOP"]); }
                        }
                        else
                        {
                            pcount = pcount + 1;
                            ptien = ptien + Convert.ToInt64(drv_khnoptien["TTIEN_NOP"].ToString());
                        }
                    }
                    lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                    lblTongtien.Content = "Tổng tiền:" + ptien;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }
       
        public void showMessErrorOnView(string str)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 15000, str, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }

        public void showMessSucOnView(string str)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 15000, str, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
        }

        public void showMessErrorOnView(string str, object errorObjec)
        {
            Component.Validate.ValidationManager.showMessOnView(this, errorObjec, str, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }

        private void btnin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //ds_KHNOPTIEN.WriteXmlSchema("D:\\Projects\\NPCUS\\ATM\\DSKHNOPTIENTNGAN.xsd");
                rptDSThutienATM_TNGAN rpt = new rptDSThutienATM_TNGAN();
                rpt.SetDataSource(ds_KHNOPTIEN.Tables["DSCHAMNO"]);
                rpt.SetParameterValue("pdenngay", pdenngay);
                rpt.SetParameterValue("ptungay", ptungay);
                rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                rpt.SetParameterValue("pthanght", i16ThangHT);
                rpt.SetParameterValue("pnamht", i16NamHT);
                UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "NPC-ATM");
                
            }
            catch (Exception ex)
            {showMessErrorOnView(ex.ToString()); return; }
        }
      
        public void loadgrid()
        { 
            try
            {
                ds_KHNOPTIEN.Clear();
                ds_KHNOPTIEN = GlobalServices.Service_NPCUS.get_dsKH_thuATM(strMadviqly, strmanh, strmapttt, ptungay, pdenngay, int_ttrang_no);
                if (ds_KHNOPTIEN != null)
                {
                    if (ds_KHNOPTIEN.Tables.Count > 0)
                    {
                        if (ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count > 0)
                        {
                            dgrdsthutien.ItemsSource = ds_KHNOPTIEN.Tables[0].DefaultView;
                            ds_KHNOPTIEN.Tables[0].DefaultView.AllowNew = false;
                           
                        }
                    }
                }
            
                for (Int32 i = 0; i < ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count; i++)
                {
                    ptien = ptien + Convert.ToInt64(ds_KHNOPTIEN.Tables["DSTHU"].Rows[i]["TTIEN_NOP"]);
                }
                ptcount = ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count;
                pcount = ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count;
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
            
                btnin.IsEnabled = true;
            }
            catch
            {}
        }
 
    }
}
